//
//  HomeViewController.m
//  Share_Home_F
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "HomeViewController.h"

#import "AFHTTPRequestOperationManager.h"
#import "BaseCollectionViewCell.h"
#import "MJRefresh.h"
#import "MJRefreshFooterView.h"
#import "MJRefreshHeaderView.h"
#import "UIView+Extension.h"
#import "DOPDropDownMenu.h"
#import "UIImageView+WebCache.h"
#import "PictureModel.h"
#import "PrefixHeader.pch"
#import "UIView+Extension.h"
#import "HomeDetailModel.h"
#import "PictureScrollViewController.h"



@interface HomeViewController ()<DOPDropDownMenuDataSource, DOPDropDownMenuDelegate>
{
    NSInteger number;
}
@property (nonatomic, retain)NSMutableArray *modelArr;
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) NSArray *spare; // 空间分类
@property (nonatomic, retain) NSMutableArray *spareone;
@property (nonatomic, retain) NSArray *arrangement; // 户型分类

@property (nonatomic, retain) UIActivityIndicatorView *indicatorView;

@end

@implementation HomeViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.title = @"图库";
        number = 1;
        self.modelArr = [NSMutableArray array];
        
        // 添加下拉按钮分类数据
        self.spare =@[@"空间", @"客厅", @"卧室", @"卫浴", @"书房", @"其他", @"玄关", @"儿童房", @"阳台", @"外景", @"衣帽间"];
        self.arrangement = @[@"户型", @"小户型", @"公寓", @"别墅", @"大户型", @"复式", @"中户型", @"两房一厅", @"一房一厅", @"住宅小区", @"三房一厅", @"两房两厅", @"三房两厅", @"一房二厅", @"跃层", @"四房两厅", @"四房一厅"];
        
        // 添加下拉菜单
        DOPDropDownMenu *menu = [[DOPDropDownMenu alloc] initWithOrigin:CGPointMake(0, 0) andHeight:44];
        menu.delegate = self;
        menu.dataSource = self;
        [self.view addSubview:menu];

    }
    
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self dataJSON];
    [self.collectionView registerClass:[BaseCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    self.collectionView.frame = CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height - 44);
    [self addFooter];
//    [self dataSpareJson];
//    [self datakindOfclassJson];
    
}


#pragma mark - 首页数据解析
-(void)dataJSON {
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    
    NSString *str1 = @"http://pic.jiajuol.com/api/iphone/0500/pic_photo.php?act=photo_list&page=";
    NSString *str2 = [NSString stringWithFormat:@"%ld&pagesize=24&v=5.0", number];
    NSString *str = [NSString stringWithFormat:@"%@%@", str1, str2];
    
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSMutableArray *arr = responseObject;
        for (int i = 0 ; i < arr.count; i++) {
            NSMutableDictionary *dic = arr[i];
            HomeDetailModel *homeDM = [[HomeDetailModel alloc] init];
            [homeDM setValuesForKeysWithDictionary:dic];
            [self.modelArr addObject:homeDM];
            
        }
        
//        
//              for (int i = 0; i < arr.count; i++) {
//            NSMutableDictionary *dic = arr[i];
//            NSString *str = [dic objectForKey:@"mimgfile"];
//            [self.modelArr addObject:str];
//        }
    [self.collectionView reloadData];
 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
    
}
#pragma mark 空间解析数据
- (void)dataSpareJson
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
//    http://pic.jiajuol.com/api/iphone/0500/pic_photo.php?act=photo_list&page=1&pagesize=24&space_id=3&v=5.0
    NSString * str1 = @"http://pic.jiajuol.com/api/iphone/0500/pic_photo.php?act=photo_list&page=";
    NSString *str2 = [NSString stringWithFormat:@"%ld", number];
    NSString *str12 = [NSString stringWithFormat:@"%@%@", str1, str2];
    NSLog(@"str12 == %@", str12);
    NSString *str3 = @"&pagesize=24&space_id=";
    NSString *str4 = [NSString stringWithFormat:@"%ld&v=5.0", number];
    NSString *str34 = [NSString stringWithFormat:@"%@%@", str3, str4];
    NSLog(@"str34 == %@", str34);
    NSString *str = [NSString stringWithFormat:@"%@%@", str12, str34];
    NSLog(@"%@", str);
    
    
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *arr = responseObject;
        NSLog(@"%@", arr);

        for (int i = 0 ; i < arr.count; i++) {
            NSMutableDictionary *dic = arr[i];
            HomeDetailModel *homeDM = [[HomeDetailModel alloc] init];
            [homeDM setValuesForKeysWithDictionary:dic];
            [self.modelArr addObject:homeDM];
            
        }
        [self.collectionView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
    
}
-(void)addHeader {
    [self.collectionView addHeaderWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.collectionView reloadData];
            
            [self.collectionView headerEndRefreshing];
        });
    }];
    [self.collectionView headerBeginRefreshing];
}

-(void)addFooter {
    [self.collectionView addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            number++;
            [self dataJSON];
            [self.collectionView reloadData];
            
            [self.collectionView footerEndRefreshing];
        });
    }];
}
#pragma mark - 实现collection代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.modelArr.count;

}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BaseCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    HomeDetailModel *home = self.modelArr[indexPath.row];
  
    NSURL *url = [NSURL URLWithString:home.bimgfile];
    [cell.myImageView sd_setImageWithURL:url];
    
    return cell;
}
// 实现点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PictureScrollViewController *picView = [[PictureScrollViewController alloc] init];
    picView.arr = self.modelArr;
    picView.idNum = [self.modelArr[indexPath.item] id];
    [self.navigationController pushViewController:picView animated:YES];
    [picView release];
   
}

#pragma mark - 通过scrollView的方法实现自动隐藏和弹出tabBar

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.tabBarController.tabBar.hidden= YES;

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.tabBarController.tabBar.hidden = NO;

}

#pragma mark - 实现Dop代理方法
- (NSInteger)numberOfColumnsInMenu:(DOPDropDownMenu *)menu
{
    return 2;
}

- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column
{
    if (column == 0) {
        return self.spare.count;
    }  else {
        return self.arrangement.count;
    }
}

- (NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath
{
    if (indexPath.column == 0) {
        return self.spare[indexPath.row];
    } else {
        return self.arrangement[indexPath.row];
    }
}




@end
